package com.fnzn.entranceGuard.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Date;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.fnzn.entranceGuard.service.IGuardRoleDeviceService;
import com.github.pig.common.vo.MenuVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pig.common.constant.CommonConstant;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.fnzn.entranceGuard.entity.GuardRoleDevice;
import com.github.pig.common.web.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sxf
 * @since 2018-06-26
 */
@RestController
@RequestMapping("/guardRoleDevice")
public class GuardRoleDeviceController extends BaseController {

    @Autowired
    private IGuardRoleDeviceService guardRoleDeviceService;


    /**
     * 通过角色查询设备号
     *
     * @param roleId 权限ID
     * @return GuardRoleDevice
     */
    @GetMapping("/getByRole")
    public List<Integer> getByRole(@RequestParam("roleId") String roleId) {
        try{
            EntityWrapper entityWrapper = new EntityWrapper<GuardRoleDevice>();
            entityWrapper.eq("role_id",roleId);
            List<GuardRoleDevice> deviceList = guardRoleDeviceService.selectList(entityWrapper);
            List<Integer> list = new ArrayList<>();
            for (GuardRoleDevice device : deviceList) {
                String[] ids = device.getDeviceIds().split(",");
                for (String id : ids ){
                    list.add(Integer.parseInt(id));
                }
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
       return null;
    }

    /**
     * 添加
     * @return success/false
     */
    @PostMapping("/guardRoleUpd")
    public R<Boolean> guardRoleUpd(Integer roleId, @RequestParam("deviceIds[]") String[] deviceIds) {
        return new R<>(guardRoleDeviceService.updateByRoleId(roleId,deviceIds));
    }

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return GuardRoleDevice
    */
    @GetMapping("/{id}")
    public R<GuardRoleDevice> get(@PathVariable Integer id) {
        return new R<>(guardRoleDeviceService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return guardRoleDeviceService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 添加
     * @param  guardRoleDevice  实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> add(@RequestBody GuardRoleDevice guardRoleDevice) {
        return new R<>(guardRoleDeviceService.insert(guardRoleDevice));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Integer id) {
        GuardRoleDevice guardRoleDevice = new GuardRoleDevice();
        guardRoleDevice.setId(id);
        return new R<>(guardRoleDeviceService.updateById(guardRoleDevice));
    }

    /**
     * 编辑
     * @param  guardRoleDevice  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody GuardRoleDevice guardRoleDevice) {
        return new R<>(guardRoleDeviceService.updateById(guardRoleDevice));
    }
}
